package cn.com.infcn.spider.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static long[] getTimeDifference(Date startDate,Date endDate){
		long nd = 1000*24*60*60;//一天的毫秒数
		long nh = 1000*60*60;//一小时的毫秒数
		long nm = 1000*60;//一分钟的毫秒数
		long ns = 1000;//一秒钟的毫秒数long {
		//获得两个时间的毫秒时间差异
		long l=endDate.getTime()-startDate.getTime();
		long day = l/nd;//计算差多少天
		long hour = l%nd/nh;//计算差多少小时
		long min = l%nd%nh/nm;//计算差多少分钟
		long second = l%nd%nh%nm/ns;//计算差几秒;
		long[] dateArray = new long[4];
		if(endDate != null && startDate != null){
			if(endDate.after(startDate)){
				if(day > 0){
					dateArray[0] = day;
				}
				if(hour > 0){
					dateArray[1] = hour;
				}
				if(min > 0){
					dateArray[2] = min;
				}
				if(second > 0){
					dateArray[3] = second;
				}
			}
		}
		return dateArray;
	}
    public static String dateToString(Date date, String format) {
        if(date == null){
            return null;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Date stringToDate(String dateStr, String format) {

        if (dateStr == null || dateStr == "") {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = null;
			try {
				date = sdf.parse(dateStr);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return date;
        }

    }
    
    /**
     * 
     * 指定的日期增加秒
     * 
     * @param date
     * @param amount
     * @return
     */
    public static Date addSecond(Date date, int amount) {

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);

        calendar.add(Calendar.SECOND, amount);

        return calendar.getTime();
    }
    
    /**
     * 
     * 指定的日期增加天数
     * 
     * @param date
     * @param amount
     * @return
     */
    public static Date addDay(Date date, int amount) {

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);

        calendar.add(Calendar.DAY_OF_YEAR, amount);

        return calendar.getTime();
    }
    /**
     * 计算从当前时间currentDate开始，满足条件dayOfWeek, hourOfDay, 
     * minuteOfHour, secondOfMinite的最近时间
     * @return
     */
  public static Calendar getEarliestWeekDate(Calendar currentDate, int dayOfWeek, int hourOfDay, int minuteOfHour, int secondOfMinite) {
        //计算当前时间的WEEK_OF_YEAR,DAY_OF_WEEK, HOUR_OF_DAY, MINUTE,SECOND等各个字段值
        int currentWeekOfYear = currentDate.get(Calendar.WEEK_OF_YEAR);
        int currentDayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK);
        int currentHour = currentDate.get(Calendar.HOUR_OF_DAY);
        int currentMinute = currentDate.get(Calendar.MINUTE);
        int currentSecond = currentDate.get(Calendar.SECOND);
  
        //如果输入条件中的dayOfWeek小于当前日期的dayOfWeek,则WEEK_OF_YEAR需要推迟一周
        boolean weekLater = false;
        if (dayOfWeek < currentDayOfWeek) {
            weekLater = true;
        } else if (dayOfWeek == currentDayOfWeek) {
            //当输入条件与当前日期的dayOfWeek相等时，如果输入条件中的
            //hourOfDay小于当前日期的
            //currentHour，则WEEK_OF_YEAR需要推迟一周   
            if (hourOfDay < currentHour) {
                weekLater = true;
            } else if (hourOfDay == currentHour) {
                 //当输入条件与当前日期的dayOfWeek, hourOfDay相等时，
                 //如果输入条件中的minuteOfHour小于当前日期的
                //currentMinute，则WEEK_OF_YEAR需要推迟一周
                if (minuteOfHour < currentMinute) {
                    weekLater = true;
                } else if (minuteOfHour == currentSecond) {
                     //当输入条件与当前日期的dayOfWeek, hourOfDay， 
                     //minuteOfHour相等时，如果输入条件中的
                    //secondOfMinite小于当前日期的currentSecond，
                    //则WEEK_OF_YEAR需要推迟一周
                    if (secondOfMinite < currentSecond) {
                        weekLater = true;
                    }
                }
            }
        }
        if (weekLater) {
            //设置当前日期中的WEEK_OF_YEAR为当前周推迟一周
            currentDate.set(Calendar.WEEK_OF_YEAR, currentWeekOfYear + 1);
        }
        // 设置当前日期中的DAY_OF_WEEK,HOUR_OF_DAY,MINUTE,SECOND为输入条件中的值。
        currentDate.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        currentDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
        currentDate.set(Calendar.MINUTE, minuteOfHour);
        currentDate.set(Calendar.SECOND, secondOfMinite);
        return currentDate;
  }
  /**
   * 计算从当前时间currentDate开始，满足条件dayOfWeek, hourOfDay, 
   * minuteOfHour, secondOfMinite的最近时间
   * @return
   */
public static Calendar getEarliestDayDate(Calendar currentDate,int dayOfWeek,int hourOfDay, int minuteOfHour, int secondOfMinite) {
      //计算当前时间的WEEK_OF_YEAR,DAY_OF_WEEK, HOUR_OF_DAY, MINUTE,SECOND等各个字段值
      int currentHour = currentDate.get(Calendar.HOUR_OF_DAY);
      int currentMinute = currentDate.get(Calendar.MINUTE);
      int currentSecond = currentDate.get(Calendar.SECOND);
      //如果输入条件中的dayOfWeek小于当前日期的dayOfWeek,则WEEK_OF_YEAR需要推迟一天
      boolean dayLater = false;
      //如果输入条件中的
      //hourOfDay小于当前日期的currentHour，则currentDayOfWeek需要推迟一天   
      if (hourOfDay < currentHour) {
    	  dayLater = true;
      } else if (hourOfDay == currentHour) {
           //当输入条件与当前日期的currentHour, hourOfDay相等时，
           //如果输入条件中的minuteOfHour小于当前日期的
          //currentMinute，则currentDayOfWeek需要推迟一天
          if (minuteOfHour < currentMinute) {
        	  dayLater = true;
          } else if (minuteOfHour == currentSecond) {
               //当输入条件与当前日期的dayOfWeek, hourOfDay， 
               //minuteOfHour相等时，如果输入条件中的
               //secondOfMinite小于当前日期的currentSecond，
               //则currentDayOfWeek需要推迟一天
              if (secondOfMinite < currentSecond) {
            	  dayLater = true;
              }
          }
      }
      if (dayLater) {
          //设置当前日期中的WEEK_OF_YEAR为当前周推迟一周
    	  currentDate.set(Calendar.DAY_OF_WEEK, dayOfWeek + 1);
      }
      // 设置当前日期中的HOUR_OF_DAY,MINUTE,SECOND为输入条件中的值。
      currentDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
      currentDate.set(Calendar.MINUTE, minuteOfHour);
      currentDate.set(Calendar.SECOND, secondOfMinite);
      return currentDate;
}
	public static void main(String[] args) {
		System.out.println(new Date("Tue Sep 29 18:33:26 CST 2015"));
	   /* Date nextFetchDate = stringToDate("2000-11-11 12:12:12", "yyyy-MM-ddHH:mm:ss");
	    Date newNextFetchDate = DateUtil.addSecond(nextFetchDate, 1);
	    int i=0;
	    while(true){
	        newNextFetchDate = DateUtil.addDay(newNextFetchDate, 1);
	        if(newNextFetchDate.after(new Date())){
	            break;
	        }
	    }
	    System.out.println(newNextFetchDate);*/
	}
}
